define(function(require, exports){
	var pubjs  = require('pubjs');
	var util   = require('util');
	var view   = require('@base/view');
	var $      = require('jquery');
	var dspUtil = require('dspUtil');
	var common = require('modules/common');

	// 渠道
	var Channel = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				view_model: {
					eventSelected:this.eventSelected
				},
				show_hasSelect:true
			});
			// 缓存后端返回来的ids
			this.$ids = [];
			this.$channel = []
			this.$data = null;
			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();

			$([
				'<div class="pd20">',
					'<span pub-name="importBtn" pub-mod="@base/common/input.button"/>',
					'<span pub-name="deleteBtn" pub-mod="@base/common/input.button"/>',
					'<div class="mt15" pub-name="grid" pub-mod="@base/grid.baseNoDate"/>',
				'</div>'
			].join('')).appendTo(el);
			// 已选
			$([
				'<span class="selected">',
				'<input class="cur_p" type="checkbox" ms-click="eventSelected">',
				'<span class="ml5">'+ LANG('显示已选') + '</span>',
				'</span>'
			].join('')).appendTo(el);
			this.$selectEl = el.find('.selected').css(
				{
					'position': 'absolute',
					'top': '84px',
					'right': '20px'
				}
			);
			if (!this.getConfig('show_hasSelect'))
			{
				this.$selectEl.hide();
			}
			self.buildFromTemplate({
				deleteBtn: {
					'class': 'btn btn-danger mr10',
					'value': LANG('清除勾选')
				},
				importBtn: {
					'class': 'btn btn-default mr10',
					'value': LANG('导入渠道')
				},
				grid: {
					'cols': [
						{"name":"_id", "text":LANG("渠道ID"), 'width': 80, 'align': 'center'}
						,{"name": "Name","text":LANG("渠道名称"),"align":'left', 'width': 300, 'render': ''}
					],
					'url':'channelInfo/listById',
					'gridName':'channel',
					'opClick': true,
					'default_sort': false,
					'hasSearch': true,
					'hasSubGrid': false,
					'hasAmount': false,
					'hasTab': false,
					'hasExport': false,
					'pager': {'size': 10},
					"hasSelect": true,
					'auto_load': true,
					'reqType': 'websocket'
				}
			});
			this.createAsync('adPopwin', 'admin/premium/popwin/import.adImport');
		}
		,afterBuildTemplate: function(){
			if(this.$data){
				this.setData(this.$data);
			}
		}
		,eventSelected: function()
		{
			this.$selected = !this.$selected;
			var ids = [];
			// 勾上已选
			if (this.$selected) {
				ids = this.getData();
				if (ids.length) {
					this.$.grid.setParam({'ids': ids}).load();
				}
				else {
					this.$.grid.setData([]);
				}
			}
			else {
				this.$.grid.setParam({'ids': null}).load();
			}
			return false;
		}
		,onInputClick: function(ev){
			var name = ev.name;
			switch (name){
				case 'deleteBtn':
					this.deleteAll();
					break;
				case 'importBtn':
					this.$.adPopwin.reset().show();
					break;
			}
		}
		,onAdImportOk: function(ev){
			var cs = this.$;
			var ids = util.clone(this.$.grid.getSelectRowIds());
			var data = ev.param.items;
			var currentIds = []
			data.forEach(function(item){
				var id = +item.id;
				currentIds.push(id);
			});
			// 直接用将两个数组用concat的话，只能合并，不能去重，因为他们的_id不是数字，是int64
			if (currentIds) {
				for (var i = currentIds.length-1; i>=0; i--) {
					var currentId = currentIds[i];
					for (var j = 0; j<ids.length; j++) {
						if (currentId === ids[j]) {
							currentIds.splice(i, 1);
							break;
						}
					}
				}
				this.$ids = currentIds.concat(ids);
			}
			else{
				pubjs.alert(LANG('找不到相应的渠道'));
				return false;
			}
			this.$channel = [];
			// 在表格中选择
			cs.grid.setSelectRowIds(this.$ids);
			return false;
		}
		,deleteAll: function(){
			var cs = this.$;
			cs.grid.setSelectRowIds([]);
			// 清除缓存的ids
			this.$ids = [];
			this.$channel = [];
			return this;
		}
		,getData: function(){
			return this.$.grid.getSelectRowIds();
		}
		,setData: function(data){
			this.$data = data;
			if(data && this.get('grid')){
				var self = this;
				var cs = self.$;
				// 更新缓存的已导入的数据
				this.$ids = data;
				this.$channel = [];
				cs.grid.setSelectRowIds(this.$ids)
			}
			return self;
		}
		,reset: function(){
			// 重置缓存的导入的数据
			this.$ids = [];
			if(this.$.grid){
				this.$data = null;
				this.$selectEl.find('>input').attr('checked', false);
				this.$selected = false;
				this.$.grid.setParam({'ids': null});
				this.$.grid.reload();
				this.$.grid.setSelectRowIds([]);
			}
			return this;
		}
	});
	exports.channel = Channel;

	// 广告位
	var Spot = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				view_model: {
					eventSelected:this.eventSelected
				},
				show_hasSelect:true
			});
			// 缓存后端返回来的ids
			this.$ids = [];
			this.$data = null;
			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();

			$([
				'<div class="pd20">',
				'<span pub-name="importBtn" pub-mod="@base/common/input.button"/>',
				'<span pub-name="deleteBtn" pub-mod="@base/common/input.button"/>',
				'<div class="mt15" pub-name="grid" pub-mod="@base/grid.baseNoDate"/>',
				'</div>'
			].join('')).appendTo(el);
			// 已选
			$([
				'<span class="selected">',
				'<input class="cur_p" type="checkbox" ms-click="eventSelected">',
				'<span class="ml5">'+ LANG('显示已选') + '</span>',
				'</span>'
			].join('')).appendTo(el);
			this.$selectEl = el.find('.selected').css(
				{
					'position': 'absolute',
					'top': '84px',
					'right': '20px'
				}
			);
			if (!this.getConfig('show_hasSelect'))
			{
				this.$selectEl.hide();
			}
			self.buildFromTemplate({
				deleteBtn: {
					'class': 'btn btn-danger mr10',
					'value': LANG('清除所选')
				},
				importBtn: {
					'class': 'btn btn-default mr10',
					'value': LANG('导入广告位')
				},
				grid: {
					'cols': [
						{name: '_id', text: LANG('广告位ID'), align: 'center'},
						{name: 'Name', text: LANG('广告位名称'), align: 'left'},
						{name: 'Size', text: LANG('广告位尺寸'), align: 'left'},
					],
					'opClick': true,
					'default_sort': false,
					'hasSearch': true,
					'hasSubGrid': false,
					'hasAmount': false,
					'hasTab': false,
					"hasSelect": true,
					'hasExport': false,
					'pager': {'size': 10},
					'url':'spot/list',
					'auto_load': true,
					'reqType': 'websocket'
				}
			});

			this.createAsync('adPopwin', 'admin/premium/popwin/import.spotmport');
		}
		,afterBuildTemplate: function(){
			if(this.$data){
				this.setData(this.$data);
			}
		}
		,eventSelected: function()
		{
			this.$selected = !this.$selected;
			var ids = [];
			// 勾上已选
			if (this.$selected) {
				ids = this.getData();
				if (ids.length) {
					this.$.grid.setParam({'ids': ids}).load();
				}
				else {
					this.$.grid.setData([]);
				}
			}
			else {
				this.$.grid.setParam({'ids': null}).load();
			}
			return false;
		}
		,onInputClick: function(ev){
			var name = ev.name;
			switch (name){
				case 'deleteBtn':
					this.deleteAll();
					break;
				case 'importBtn':
					this.$.adPopwin.reset().show();
					break;
			}
		}
		,onAdImportOk: function(ev){
			var cs = this.$;
			var ids = util.clone(this.$.grid.getSelectRowIds());
			var data = ev.param.items;
			var currentIds = [];
			data.forEach(function(item){
				var id = +item.id || item._id;
				currentIds.push(id);
			});
			// 直接用将两个数组用concat的话，只能合并，不能去重，因为他们的_id不是数字，是int64
			if (currentIds) {
				for (var i = currentIds.length-1; i>=0; i--) {
					var currentId = currentIds[i];
					for (var j = 0; j<ids.length; j++) {
						if (currentId === ids[j]) {
							currentIds.splice(i, 1);
							break;
						}
					}
				}
				this.$ids = currentIds.concat(ids);
			}
			else{
				pubjs.alert(LANG('找不到相应广告位'));
				return false;
			}
			// 在表格中选择
			cs.grid.setSelectRowIds(this.$ids);
			return false;
		}
		,deleteAll: function(){
			var cs = this.$;
			cs.grid.setSelectRowIds([]);
			// 清除缓存的ids
			this.$ids = [];
			return this;
		}
		,getData: function(){
			return this.$.grid.getSelectRowIds();
		}
		,setData: function(data){
			this.$data = data;
			if(data && this.get('grid')){
				var self = this;
				var cs = self.$;
				// 更新缓存的已导入的数据
				this.$ids = data;
				cs.grid.setSelectRowIds(this.$ids)
			}
			return self;
		}
		,reset: function(){
			// 重置缓存的导入的数据
			this.$ids = [];
			if(this.$.grid){
				this.$data = null;
				this.$selectEl.find('>input').attr('checked', false);
				this.$selected = false;
				this.$.grid.setParam({'ids': null});
				this.$.grid.reload();
				this.$.grid.setSelectRowIds([]);
				// 已选去除勾选状态
			}
			return this;
		}
	});
	exports.spot = Spot;

	// 订单
	var Deal = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				view_model: {
					eventSelected:this.eventSelected
				},
				show_hasSelect:true
			});

			// 缓存后端返回来的ids
			this.$ids = [];
			this.$channel = []
			this.$data = null;

			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();

			$([
				'<div class="pd20">',
				'<span pub-name="importBtn" pub-mod="@base/common/input.button"/>',
				'<span pub-name="deleteBtn" pub-mod="@base/common/input.button"/>',
				'<div class="mt15" pub-name="grid" pub-mod="@base/grid.baseNoDate"/>',
				'</div>'
			].join('')).appendTo(el);
			// 已选
			$([
				'<span class="selected">',
				'<input class="cur_p" type="checkbox" ms-click="eventSelected">',
				'<span class="ml5">'+ LANG('显示已选') + '</span>',
				'</span>'
			].join('')).appendTo(el);
			this.$selectEl = el.find('.selected').css(
				{
					'position': 'absolute',
					'top': '84px',
					'right': '20px'
				}
			);
			if (!this.getConfig('show_hasSelect'))
			{
				this.$selectEl.hide();
			}
			self.buildFromTemplate({
				deleteBtn: {
					'class': 'btn btn-danger mr10',
					'value': LANG('删除所有')
				},
				importBtn: {
					'class': 'btn btn-default mr10',
					'value': LANG('导入订单')
				},
				grid: {
					'cols': [
						{name: '_id', text: LANG('订单id'), align: 'center'},
						{name: 'Name', text: LANG('订单名称'), align: 'left'},
					],
					'opClick': true,
					'default_sort': false,
					'hasSearch': true,
					'hasSubGrid': false,
					'hasAmount': false,
					'hasTab': false,
					'hasExport': false,
					'pager': {'size': 10},
					'hasSelect':true,
					'url':'deal/list',
					'auto_load': true,
					'reqType': 'websocket'
				}
			});

			this.createAsync('adPopwin', 'admin/premium/popwin/import.orderImport');
		}
		,afterBuildTemplate: function(){
			if(this.$data){
				this.setData(this.$data);
			}
		}
		,eventSelected: function()
		{
			this.$selected = !this.$selected;
			var ids = [];
			// 勾上已选
			if (this.$selected) {
				ids = this.getData();
				if (ids.length) {
					this.$.grid.setParam({'ids': ids}).load();
				}
				else {
					this.$.grid.setData([]);
				}
			}
			else {
				this.$.grid.setParam({'ids': null}).load();
			}
			return false;
		}
		,onInputClick: function(ev){
			var name = ev.name;
			switch (name){
				case 'deleteBtn':
					this.deleteAll();
					break;
				case 'importBtn':
					this.$.adPopwin.reset().show();
					break;
			}
		}
		,onAdImportOk: function(ev){
			var cs = this.$;
			var ids = util.clone(this.$.grid.getSelectRowIds());
			var data = ev.param.items;
			var currentIds = [];
			data.forEach(function(item){
				var id = +item.id || item._id;
				currentIds.push(id);
			});
			// 直接用将两个数组用concat的话，只能合并，不能去重，因为他们的_id不是数字，是int64
			if (currentIds) {
				for (var i = currentIds.length-1; i>=0; i--) {
					var currentId = currentIds[i];
					for (var j = 0; j<ids.length; j++) {
						if (currentId === ids[j]) {
							currentIds.splice(i, 1);
							break;
						}
					}
				}
				this.$ids = currentIds.concat(ids);
			}
			else{
				pubjs.alert(LANG('找不到相应订单'));
				return false;
			}
			// 在表格中选择
			cs.grid.setSelectRowIds(this.$ids);
			return false;
		}
		,deleteAll: function(){
			var cs = this.$;
			cs.grid.setSelectRowIds([]);
			// 清除缓存的ids
			this.$ids = [];
			return this;
		}
		,getData: function(){
			return this.$.grid.getSelectRowIds();
		}
		,setData: function(data){
			this.$data = data;
			if(data && this.get('grid')){
				var self = this;
				var cs = self.$;
				// 更新缓存的已导入的数据
				this.$ids = data;
				cs.grid.setSelectRowIds(this.$ids)
			}
			return self;
		}
		,reset: function(){
			// 重置缓存的导入的数据
			this.$ids = [];
			if(this.$.grid){
				this.$data = null;
				this.$selectEl.find('>input').attr('checked', false);
				this.$selected = false;
				this.$.grid.setParam({'ids': null});
				this.$.grid.reload();
				this.$.grid.setSelectRowIds([]);
			}
			return this;
		}
	});
	exports.deal = Deal;

});